﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using SD.IBUS;
using SD.BUS;
using SD.Entities;
using SD.Common;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace SD.UI
{
    public partial class AddSupportingMaterial : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Check when this page is edit supporting material page
            if (!IsPostBack)
            {
                TabContainer1.ActiveTabIndex = Convert.ToInt32(Session["indexTab"]);
                BindDDLType();

                if (!String.IsNullOrEmpty(Request.QueryString["SupportingMaterialID"]))
                {
                    ISupportingMaterialBIZ smBIZ = new SupportingMaterialBIZ();
                    SupportingMaterial sm = smBIZ.GetById(Request.QueryString["SupportingMaterialID"]);
                    smBIZ.SetActive(Request.QueryString["SupportingMaterialID"], true);
                    tbxUrl.Text = sm.URL;
                    tbxDescription.Text = sm.Description;
                    for (int i = 0; i < ddlType.Items.Count; i++)
                    {
                        if (String.Equals(ddlType.Items[i].Text, sm.Type.RefValue))
                        {
                            ddlType.SelectedIndex = i;
                            break;
                        }
                    }
                    tbxAddedBy.Text = sm.AddedBy.Account;
                    tbxAddedDate.Text = sm.AddedDate.ToShortDateString();
                    btnMSInActive.Visible = true;
                }
                else
                {
                    User user = (User)Session["Username"];
                    tbxAddedBy.Text = user.Account;
                    tbxAddedDate.Text = DateTime.Now.ToShortDateString();
                    btnMSInActive.Visible = false;
                }
            }


        }

        protected void btnSMSave_Click(object sender, EventArgs e)
        {
            ISupportingMaterialBIZ iSupportingMaterialBIZ = new SupportingMaterialBIZ();
            SupportingMaterial supportingMaterial = new SupportingMaterial();

            // Editting supporting material
            if (!String.IsNullOrEmpty(Request.QueryString["SupportingMaterialID"]))
            {
                supportingMaterial.URL = tbxUrl.Text;
                supportingMaterial.Description = tbxDescription.Text;
                supportingMaterial.Type.RefId = ddlType.SelectedValue.ToString();
                supportingMaterial.SupportingMaterialId = Request.QueryString["SupportingMaterialID"];
                iSupportingMaterialBIZ.Update(supportingMaterial);
                ShowMessageAndRedirect(INFO_SAVE_SUCCESSFUL, "EditOrganization.aspx?orgId=" + Request.QueryString["orgId"] + "&ActiveTabIdx=4" + "&smCurrentPage=" + Request.QueryString["smCurrentPage"] + "&smInactive=" + Request.QueryString["smInactive"]);
            }
            // Adding supporting material
            else
            {
                supportingMaterial.URL = tbxUrl.Text;
                supportingMaterial.Description = tbxDescription.Text;

                supportingMaterial.Type.RefId = ddlType.SelectedValue.ToString();
                supportingMaterial.AddedBy = (User)Session["Username"];
                supportingMaterial.AddedDate = DateTime.Now;
                supportingMaterial.IsActive = true;
                supportingMaterial.Organization.OrgId = Request.QueryString["orgId"];
                iSupportingMaterialBIZ.Insert(supportingMaterial);
                ShowMessageAndRedirect(INFO_INSERT_SUCCESSFUL, "EditOrganization.aspx?orgId=" + Request.QueryString["orgId"] + "&ActiveTabIdx=4" + "&smCurrentPage=" + Request.QueryString["smCurrentPage"] + "&smInactive=" + Request.QueryString["smInactive"]);
            }
        }

        protected void btnSMBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("EditOrganization.aspx?orgId=" + Request.QueryString["orgId"] + "&ActiveTabIdx=4" + "&smCurrentPage=" + Request.QueryString["smCurrentPage"] + "&smInactive=" + Request.QueryString["smInactive"]);
        }

        protected void TabContainer1_ActiveTabChanged(object sender, EventArgs e)
        {
            Session["indexTab"] = TabContainer1.ActiveTabIndex;
        }

        private void BindDDLType()
        {
            IReferenceDataBIZ rfBIZ = new ReferenceDataBIZ();

            List<ReferenceData> list = rfBIZ.GetByRefCode("smType");

            DataTable dt = new DataTable();
            dt.Columns.Add("RefID");
            dt.Columns.Add("RefCode");
            dt.Columns.Add("RefValue");

            if (list.Count > 0)
            {
                foreach (ReferenceData item in list)
                {
                    DataRow row = dt.NewRow();
                    row["RefID"] = item.RefId;
                    row["RefCode"] = item.RefCode;
                    row["RefValue"] = item.RefValue;

                    dt.Rows.Add(row);
                }
            }

            ddlType.DataSource = dt;
            ddlType.DataTextField = "RefValue";
            ddlType.DataValueField = "RefID";
            ddlType.DataBind();
        }

        protected void btnMSInActive_Click(object sender, EventArgs e)
        {
            ISupportingMaterialBIZ smBIZ = new SupportingMaterialBIZ();
            string smId = Request.QueryString["SupportingMaterialID"];
            bool result = smBIZ.SetActive(smId, false);

            if (result)
            {
                ShowMessage("This supporting material is inactive now");
            }
        }
    }
}
